package com.cne.gmail.Gmail.controller;

import com.cne.gmail.Gmail.dao.AddressListRepository;
import com.cne.gmail.Gmail.model.AddressList;
import com.cne.gmail.Gmail.model.MessageModel;
import com.cne.gmail.Gmail.response.GmailServiceResponse;
import com.google.common.collect.Lists;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * 通讯录接口
 */
@Api(description ="通讯录管理接口")
@RestController
@RequestMapping("addressList")
@Log4j2
public class AddressListController {
    //添加通讯录
    @Autowired
    AddressListRepository addressListRepository;
    /**
     * 添加通讯录
     * @param name
     * @param emailAddress
     * @param group
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "通讯录添加",notes = "通讯录添加")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "name",value = "用户名",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "emailAddress",value = "邮箱地址",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "group",value = "分组信息",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "me",value = "当前用户邮箱地址",required = true,dataType = "String")
    })
    public GmailServiceResponse insert(@RequestParam String me,@RequestParam String name,@RequestParam String emailAddress,@RequestParam(value = "group",required = false) String group ){
        GmailServiceResponse response=new GmailServiceResponse();
        AddressList addressList=new AddressList();
        addressList.setMe(me);
        addressList.setName(name);
        addressList.setAddress(emailAddress);
        addressList.setGroup(group);
        AddressList result = addressListRepository.findByMeAndAddressAndGroup(me,emailAddress,group);
        if (result!=null){
            response.setMsg("添加失败，添加用户人和分组已存在！");
            return response;
        }else {
            try {
                AddressList save = addressListRepository.save(addressList);
                response.setSuccess(true);
                response.setCode(200);
                response.setMsg("通讯录添加成功");
                response.setData(save);
                return response;
            }catch (Exception e){
                response.setMsg("添加失败");
                log.info(e.getMessage());
                return response;
            }
        }
    }

    /**
     * 删除通讯录
     * @param ids
     * @return
     */
    @DeleteMapping("delete")
    @ApiOperation(value = "删除通讯录",notes = "删除通讯录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "ids",value = "要删除的id集合",required = true,dataType = "String")
    })
    public GmailServiceResponse delete(@RequestParam List<String> ids){
        GmailServiceResponse response=new GmailServiceResponse();
        try {
            for (String id:ids ){
                addressListRepository.deleteById(id);
            }
            response.setMsg("删除成功");
            return response;
        }catch (Exception e){
            response.setMsg("删除失败");
            log.info(e.getMessage());
            return response;
        }
    }
    /**
     * 查询通讯录所有信息
     * @param me
     * @return
     */
    @GetMapping("searchByMe")
    @ApiOperation(value = "查询",notes = "查询通讯录所有联系人")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "me",value = "当前用户邮箱",required = true,dataType = "String"),
    })
    public GmailServiceResponse searchByMe(@RequestParam String me ){
        GmailServiceResponse response=new GmailServiceResponse();
        Page<AddressList> result = addressListRepository.findByMe(me,null);
        if (result.getContent().size()==0){
            response.setMsg("通讯录为空");
            return  response;
        }else {
            response.setData(result);
            response.setCode(200);
            return response;
        }
     }

    /**
     * 修改通讯录信息
     * @param id
     * @param me
     * @param name
     * @param emailAddress
     * @param group
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "更新",notes = "查询通讯录所有联系人")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "me",value = "当前邮箱用户",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "name",value = "用户名",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "emailAddress",value = "邮箱地址",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "group",value = "分组信息",required = false,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "id",value = "唯一id",required = true,dataType = "String")
    })
    public GmailServiceResponse update(@RequestParam String id,@RequestParam String me,@RequestParam String name,@RequestParam String emailAddress,@RequestParam(value = "group",required = false) String group) {
        GmailServiceResponse response = new GmailServiceResponse();
        AddressList result = addressListRepository.findByMeAndAddressAndGroup(me,emailAddress,group);
        try {
            if (result != null) {
                response.setMsg("更新后的数据在数据库中已经存在，请检查后重新更新");
                return  response;
            } else {
                Optional<AddressList> optional=addressListRepository.findById(id);
                if (optional.get()==null){
                    response.setMsg("改分组在数据库中未查询到，请联系开发人员");
                    return  response;
                }else {
                    optional.get().setMe(me);
                    optional.get().setName(name);
                    optional.get().setAddress(emailAddress);
                    optional.get().setGroup(group);
                    AddressList save = addressListRepository.save(optional.get());
                    response.setCode(200);
                    response.setData(save);
                    response.setMsg("更新数据成功");
                    return response;
                }

            }
        }catch (Exception e){
            log.info(e.getMessage());
            response.setMsg("更新数据失败");
            return response;
        }
     }

    /**
     * 根据分组信息查询通讯录信息
     * @param me
     * @param group
     * @return
     */
    @GetMapping("searchByMeAndGroup")
    @ApiOperation(value = "查询",notes = "查询通讯录所有联系人")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "me",value = "当前用户邮箱",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "group",value = "分组名",required = true,dataType = "String")
    })
    public GmailServiceResponse searchByMeAndGroup(@RequestParam String me,@RequestParam String group ){
        GmailServiceResponse response=new GmailServiceResponse();
        List<AddressList> byMeAndGroup = addressListRepository.findByMeAndGroup(me, group);
        if (byMeAndGroup.size()==0){
            response.setMsg("通讯录为空");
            return  response;
        }else {
            response.setData(byMeAndGroup);
            response.setCode(200);
            return response;
        }
    }



}
